package gob.minedu.capacitacion.shared.util;

import gob.minedu.capacitacion.cxa.bean.TallerBean;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;



import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;

public class Util {
	
//	public static MethodExpression createMethodExpression(String methodExpression) {	       
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//        return facesContext.getApplication().getExpressionFactory().createMethodExpression(
//            facesContext.getELContext(), methodExpression, String.class, new Class[] {});
//    }
//	
//	public static ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
//		
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//        return facesContext.getApplication().getExpressionFactory().createValueExpression(
//            facesContext.getELContext(), valueExpression, valueType);
//    }
//	
	
	
	/**
	 * @param fecha Ejm 22/10/2009
	 * @param formato Ejm  "dd/MM/yyyy"
	 * @return Fecha en tipo Date
	 */
	public static Date convertStringToDate(String fecha, String formato){
		SimpleDateFormat formatoDelTexto = new SimpleDateFormat(formato);

		try {
			if("".equals(fecha.trim()))return null;
			return formatoDelTexto.parse(fecha);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	

	/**
	 * Convierte una fecha de tipo Date a un formato enviado como parametro
	 * @param	myDate			Fecha en formato Date
	 * @param	dd/MM/yyyy	Formato de la fecha de Salida
	 * */
	public static String convertToDateFormat(Date myDate, String customFormat){
		SimpleDateFormat simpleDateFormat = null;
		try {
			simpleDateFormat = new SimpleDateFormat(customFormat.replaceAll("mm", "MM"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return simpleDateFormat.format(myDate);
	}
	
	
	/**
	 * @return Fecha y Hora Actual en Timestamp
	 */
	public static Timestamp getNowTimestamp(){
		return new Timestamp(new Date().getTime()); 
	}
	
	
	/**
     * Utility method for creating <code>Date</code> objects.
     *
     * @param day  the date.
     * @param month  the month.
     * @param year  the year.
     *
     * @return a date.
     */
    public static Date date(final int day, final int month, final int year) {

        final Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        final Date result = calendar.getTime();
        return result;

    }
    
	/**
     * Utility method for creating <code>Date</code> objects.
     *
 
     * @param fecha en formato dd/mm/yyyy
     *
     * @return a date.
     */
    public static Date date(String fecha) {
    	
    	
    	
    	int dia = Integer.parseInt(fecha.substring(0, 2));
    	int mes = Integer.parseInt(fecha.substring(3, 5));
    	int anio = Integer.parseInt(fecha.substring(6, 10));
    	
    	final Calendar calendar = Calendar.getInstance();
        calendar.set(anio, getMonth(mes), dia);
        final Date result = calendar.getTime();
        return result;


    }
    
    private static int getMonth(int mes){
    	switch (mes) {
		case 1:
			return Calendar.JANUARY; 
		case 2:
			return Calendar.FEBRUARY; 
		case 3:
			return Calendar.MARCH; 
		case 4:
			return Calendar.APRIL; 
		case 5:
			return Calendar.MAY; 
		case 6:
			return Calendar.JUNE; 
		case 7:
			return Calendar.JULY; 
		case 8:
			return Calendar.AUGUST; 
		case 9:
			return Calendar.SEPTEMBER; 
		case 10:
			return Calendar.OCTOBER; 
		case 11:
			return Calendar.NOVEMBER; 

		default:
			return Calendar.DECEMBER;
		}
    }
    
    
    
    /**
     * Comparamos las Fechas
     * @author prios
     * @param fecha1
     * @param fechaActual
     * @return
     */
    private String compararFechasConDate(String fecha1, String fechaActual) {  
     System.out.println("Parametro String Fecha 1 = "+fecha1+"\n" +
       "Parametro String fechaActual = "+fechaActual+"\n");  
     String resultado="";
     try {
      /**Obtenemos las fechas enviadas en el formato a comparar*/
      SimpleDateFormat formateador = new SimpleDateFormat("dd/MM/yyyy"); 
      Date fechaDate1 = formateador.parse(fecha1);
      Date fechaDate2 = formateador.parse(fechaActual);
       
      System.out.println("Parametro Date Fecha 1 = "+fechaDate1+"\n" +
        "Parametro Date fechaActual = "+fechaDate2+"\n");
       
       if ( fechaDate1.before(fechaDate2) ){
       resultado= "La Fecha 1 es menor ";
       }else{
        if ( fechaDate2.before(fechaDate1) ){
         resultado= "La Fecha 1 es Mayor ";
        }else{
         resultado= "Las Fechas Son iguales ";
        } 
       }
     } catch (ParseException e) {
      System.out.println("Se Produjo un Error!!!  "+e.getMessage());
     }  
     return resultado;
    }
    
    
    /**
     * Comparamos las Fechas
     * @author prios
     * @param fecha1
     * @param fechaActual
     * @return
     */
    public static boolean fechasValidas(Date fechaIni, Date fechaFin) {  
     
     boolean resultado=false;
     try {
    	 
       if(fechaIni==null || fechaFin==null) return true;
           
      
       if ( fechaIni.before(fechaFin) ){
       resultado= true;
       }else{
        if ( fechaFin.before(fechaIni) ){
         resultado= false;
        }else{
         resultado= true;
        } 
       }
     } catch (Exception e) {
      System.out.println("Se Produjo un Error!!!  "+e.getMessage());
     }  
     return resultado;
    }
    
    
    
	
	public static IntervalCategoryDataset createDataset(List<TallerBean> talleres) {
		
		
		
		TaskSeries s1 = new TaskSeries("Programado");
		TaskSeriesCollection collection = new TaskSeriesCollection();
		
		TallerBean obj = null;
		for(int i =0; i<talleres.size();i++){
			obj = talleres.get(i);
			
			if( obj.getFecha_inicio()!=null && obj.getFecha_fin()!=null){
				s1.add(new Task("Taller "+ obj.getCodigo(),
			               new SimpleTimePeriod(Util.date(obj.getStrFechaInicio()),
			            		   Util.date(obj.getStrFechaFin()))));
			}
			
			
		}
		
		
		/*
        
        s1.add(new Task("Obtain Approval",
               new SimpleTimePeriod(Util.date(9, Calendar.APRIL, 2001),
            		   Util.date(9, Calendar.APRIL, 2001))));
        s1.add(new Task("Requirements Analysis",
               new SimpleTimePeriod(Util.date(10, Calendar.APRIL, 2001),
            		   Util.date(5, Calendar.MAY, 2001))));
        s1.add(new Task("Design Phase",
               new SimpleTimePeriod(Util.date(6, Calendar.MAY, 2001),
            		   Util.date(30, Calendar.MAY, 2001))));
        s1.add(new Task("Design Signoff",
               new SimpleTimePeriod(Util.date(2, Calendar.JUNE, 2001),
                                    Util.date(2, Calendar.JUNE, 2001))));
        s1.add(new Task("Alpha Implementation",
               new SimpleTimePeriod(Util.date(3, Calendar.JUNE, 2001),
            		   Util.date(31, Calendar.JULY, 2001))));
        s1.add(new Task("Design Review",
               new SimpleTimePeriod(Util.date(1, Calendar.AUGUST, 2001),
            		   Util.date(8, Calendar.AUGUST, 2001))));
        s1.add(new Task("Revised Design Signoff",
               new SimpleTimePeriod(Util.date(10, Calendar.AUGUST, 2001),
            		   Util.date(10, Calendar.AUGUST, 2001))));
        s1.add(new Task("Beta Implementation",
               new SimpleTimePeriod(Util.date(12, Calendar.AUGUST, 2001),
            		   Util.date(12, Calendar.SEPTEMBER, 2001))));
        s1.add(new Task("Testing",
               new SimpleTimePeriod(Util.date(13, Calendar.SEPTEMBER, 2001),
            		   Util.date(31, Calendar.OCTOBER, 2001))));
        s1.add(new Task("Final Implementation",
               new SimpleTimePeriod(Util.date(1, Calendar.NOVEMBER, 2001),
            		   Util.date(15, Calendar.NOVEMBER, 2001))));
        s1.add(new Task("Signoff",
               new SimpleTimePeriod(Util.date(28, Calendar.NOVEMBER, 2001),
            		   Util.date(30, Calendar.NOVEMBER, 2001))));

        /*final TaskSeries s2 = new TaskSeries("Actual");
        s2.add(new Task("Write Proposal",
               new SimpleTimePeriod(Util.date(1, Calendar.APRIL, 2001),
            		   Util.date(5, Calendar.APRIL, 2001))));
        s2.add(new Task("Obtain Approval",
               new SimpleTimePeriod(Util.date(9, Calendar.APRIL, 2001),
            		   Util.date(9, Calendar.APRIL, 2001))));
        s2.add(new Task("Requirements Analysis",
               new SimpleTimePeriod(Util.date(10, Calendar.APRIL, 2001),
            		   Util.date(15, Calendar.MAY, 2001))));
        s2.add(new Task("Design Phase",
               new SimpleTimePeriod(Util.date(15, Calendar.MAY, 2001),
                                    Util.date(17, Calendar.JUNE, 2001))));
        s2.add(new Task("Design Signoff",
               new SimpleTimePeriod(Util.date(30, Calendar.JUNE, 2001),
            		   Util.date(30, Calendar.JUNE, 2001))));
        s2.add(new Task("Alpha Implementation",
               new SimpleTimePeriod(Util.date(1, Calendar.JULY, 2001),
            		   Util.date(12, Calendar.SEPTEMBER, 2001))));
        s2.add(new Task("Design Review",
               new SimpleTimePeriod(Util.date(12, Calendar.SEPTEMBER, 2001),
            		   Util.date(22, Calendar.SEPTEMBER, 2001))));
        s2.add(new Task("Revised Design Signoff",
               new SimpleTimePeriod(Util.date(25, Calendar.SEPTEMBER, 2001),
            		   Util.date(27, Calendar.SEPTEMBER, 2001))));
        s2.add(new Task("Beta Implementation",
               new SimpleTimePeriod(Util.date(27, Calendar.SEPTEMBER, 2001),
            		   Util.date(30, Calendar.OCTOBER, 2001))));
        s2.add(new Task("Testing",
               new SimpleTimePeriod(Util.date(31, Calendar.OCTOBER, 2001),
            		   Util.date(17, Calendar.NOVEMBER, 2001))));
        s2.add(new Task("Final Implementation",
               new SimpleTimePeriod(Util.date(18, Calendar.NOVEMBER, 2001),
            		   Util.date(5, Calendar.DECEMBER, 2001))));
        s2.add(new Task("Signoff",
               new SimpleTimePeriod(Util.date(10, Calendar.DECEMBER, 2001),
            		   Util.date(11, Calendar.DECEMBER, 2001))));*/

       
        collection.add(s1);
        //collection.add(s2);
        
        return collection;
	}
   
	
}
